const User = require('../model/User_model')

class UserService {

    //注册
    async CreateUser(user_password, user_nickname, user_email, image, imagekey) {
        const res = await User.create({
            user_password, user_nickname, user_email, image, imagekey
        })
        console.log(res);
        return res
    }
    //查询
    async getUserInfo({ user_id, user_password, user_nickname, user_email, image, imagekey }) {
        const whereOpt = {}
        user_id && Object.assign(whereOpt, { user_id })
        user_password && Object.assign(whereOpt, { user_password })
        user_nickname && Object.assign(whereOpt, { user_nickname })
        user_email && Object.assign(whereOpt, { user_email })
        image && Object.assign(whereOpt, { image })
        imagekey && Object.assign(whereOpt, { imagekey })
        const res = await User.findOne({
            attributes: ['user_id', 'user_password', 'user_nickname', 'user_email', 'image', 'imagekey'],
            where: whereOpt
        })
        return res ? res.dataValues : null

    }
    //更新用户信息
    async updateInfo(params) {
        const { user_id, image, imagekey } = params
        const newValue = {}
        if (image) {
            newValue.image = image
        }
        if (imagekey) {
            newValue.imagekey = imagekey
        }
        const res = await User.update(
            newValue, {
            where: { user_id }
        })
        return res ? res[0] : null
    }
    async updateUserImport(params) {
        try {
            const { user_id, user_nickname, user_password } = params
            console.log(user_id, user_nickname, user_password);

            const newValue = {}
            if (user_nickname) {
                newValue.user_nickname = user_nickname
            }
            if (user_password) {
                newValue.user_password = user_password
            }
            const res = await User.update(
                newValue, {
                where: { user_id }
            })
            console.log(res);

            return res ? res[0] : null
        } catch (error) {
            console.log(error);

        }

    }
    //查找用户
}


module.exports = new UserService()